package com.wisemoresystem.seal.action.mac.stop;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.MacMoveLogService;
import com.wisemoresystem.seal.service.MacService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 停用
 * 
 * @author 冯晓东
 *
 */
@Controller("macStop")
@Scope("prototype")
@RequestMapping("/ix/mac/stop")
public class MacStopAction extends ProjBaseAction {

	@Autowired
	private MacService macService;
	@Autowired
	private MacMoveLogService macLogService;
	@Autowired
	private BankService bService;

	/**
	 * 印控仪停用列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> macList = macService.selectStartingList(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/mac/stop/list");
		mv.addObject("macList", macList);
		mv.addObject("srh_name", srh_name);
		return mv;
	}

	/**
	 * 印控仪器停用详细信息
	 * 
	 * @param mac_id
	 *            印控仪id
	 * @return
	 */
	@RequestMapping("/stop_form")
	public ModelAndView start_form(String mac_id) {

		// 判断印控仪是否正在使用
		if (macService.IsMacUsing(mac_id)) {
			super.setTipMsg("该设备正在使用中,不能停用!", Tip.Type.error);
			ModelAndView mv = new ModelAndView("redirect:list.htm");
			return mv;
		}

		int lev = bService.getBankLevel(WebTools.getCurrentBankId());
		t_mac mac = oc.findObjById(mac_id, t_mac.class);
		t_bank bank = oc.findObjById(mac.getBank_id(), t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/mac/stop/stop_form");
		mv.addObject("mac", mac);
		mv.addObject("bank", bank);
		mv.addObject("lev", lev);
		return mv;
	}

	/**
	 * 印控仪停用
	 * 
	 * @param mac_id
	 *            印控仪id
	 * @param flag_stop
	 *            停用状态
	 * @return
	 */
	@RequestMapping("/stop")
	public ModelAndView stop(String mac_id, String flag_stop) {

		boolean isStart = macLogService.stop(mac_id, flag_stop);
		if (isStart) {
			this.setTipMsg("停用成功!", Tip.Type.success);
			logT.logH("印控仪停用", t_mac.class, mac_id);
		} else {
			this.setTipMsg("停用失败!该设备状态有误!", Tip.Type.error);
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

}
